<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GrCoupon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class Coupon extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrCoupon(), function (Grid $grid) {
            $grid->column('coupon_id', 'ID')->sortable();
            $grid->column('coupon_name','活动名称');
            $grid->column('coupon_state', '状态')->radio(['上架', '下架']);
            $grid->column('coupon_min_price', '触发价格')->display(function ($e) {
                return $e . '元';
            });
            $grid->column('coupon_price')->display(function ($e) {
                return $e . '元';
            })->label();
            $grid->column('coupon_start_time', '开始时间');
            $grid->column('coupon_exit_time', '结束时间');
            $grid->column('coupon_effective_day', '有效期')->display(function ($e) {
                return $e . '天';
            })->label();
            $grid->column('coupon_type')->radio(['满减券', '立减券']);
            $grid->column('coupon_application', '优惠券范围')->radio(['通用券', '指定店铺可用', '指定商品']);
            $grid->column('coupon_astrict_type')->radio(['互斥', '共享']);
            $grid->column('coupon_stock','发放数量')->display(function ($e) {
                return $e . '张';
            });
            $grid->column('distribution_method')->radio(['自行领取', '手动发放']);
            $grid->column('receive_restrict_number', '领取次数')->display(function ($e) {
                return $e . '次';
            })->label();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->showColumnSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('coupon_name');
                $filter->equal('coupon_state')->select(['已上架', '已下架']);
                $filter->equal('coupon_type')->select(['满减券', '立减券']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrCoupon(), function (Show $show) {
            $show->field('coupon_name');
            $show->field('coupon_state');
            $show->field('coupon_min_price');
            $show->field('coupon_price');
            $show->field('coupon_effective_day', '有效期：（天）');
            $show->field('coupon_exit_time');
            $show->field('coupon_start_time', '开始时间');
            $show->field('coupon_type');
            $show->field('coupon_application', '优惠券范围');
            $show->field('coupon_astrict_type');
            $show->field('coupon_stock');
            $show->field('distribution_method');
            $show->field('receive_restrict_number', '领取限制次数');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrCoupon(), function (Form $form) {
            $form->text('coupon_name')->required();
            $form->radio('coupon_state', '上下架状态')->options(['上架', '下架'])->default(0);
            $form->text('coupon_min_price')->required()->type('number')->maxLength(5)->minLength(1);
            $form->text('coupon_price')->required()->type('number')->maxLength(5)->minLength(1);
            $form->text('coupon_effective_day', '可使用天数')->required()->type('number')->maxLength(5)->minLength(1);
            $form->datetime('coupon_start_time', '开始时间')->required();
            $form->datetime('coupon_exit_time', '结束时间')->required();
            $form->radio('coupon_type')->required()->options(['满减券', '立减券']);
            $form->radio('coupon_application')->when(3, function (Form $form) {
                $form->multipleSelectTable('coupon_goods_id', '请指定商品')//类型为指定商品优惠券就选择商品
                ->from(UserTable::make())->saving(function ($e) {
                    return $e[0];
                });
            })->options([1 => '全平台通用', 2 => '指定店铺可用', 3 => '指定商品类型可用']);
            $form->radio('coupon_astrict_type')->required()->options(['互斥', '共享']);
            $form->text('coupon_stock','发放数量')->required()->minLength(1)->maxLength(9)->type('number');
            $form->radio('distribution_method')->required()->options(['自行领取', '手动发放']);
            $form->text('receive_restrict_number', '领取限制次数')->required()->type('number')->maxLength(4);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
